package org.dromara.system.aspect;

import lombok.RequiredArgsConstructor;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.dromara.system.domain.bo.ArchivesBo;
import org.dromara.system.service.ISkyAccessLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Aspect
@Component
public class SkyLogAspect {
    @Autowired
    private ISkyAccessLogService accessLogService;

    @Pointcut("execution(* org.dromara.system.service.SkyClientService.query*(..))")
    public void queryMethods() {
    }

    @AfterReturning(pointcut = "queryMethods()", returning = "result")
    public void logQuery(JoinPoint joinPoint, Object result) {
        // 获取参数值
        Object[] args = joinPoint.getArgs();
        Object queryParam = args[0];
        String path = args[1]==null?"":args[1].toString();
        ArchivesBo bo = null;

        if(queryParam instanceof ArchivesBo) {
            bo = (ArchivesBo) queryParam;
        } else if (queryParam instanceof String){
            bo = new ArchivesBo();
            bo.setKeyword(queryParam.toString());
        }

        accessLogService.insert(bo, path, result.toString());
    }
}
